<?php
class WebapplyAction extends CommonAction {  
	private $statusArray = array(1=>'待审核',2=>'已拒绝',3=>'付费快审');

	public function __construct(){
		parent::__construct();
		
		$this->modelObject    = D('Webapply');
		$this->websitesObject = D('Websites');
		$this->webdataObject  = D('Webdata');
		
		$this->assign('statusArray', $this->statusArray);
		$this->assign('cateOption', R('WebsitesCate/getCateOption'));
    }
	
    public function index(){
		$thisPage = $this->pageInfo['p'] = I('p', 1);
		$map = array();
		$map['is_recycle'] = 2;
		
		if($this->pageInfo['name'] = I('name', '')){
			$map['name'] = array('LIKE', '%'.$this->pageInfo['name'].'%');
		}
		
		if($this->pageInfo['cate_id'] = I('cate_id', '')){
			$map['cate_id'] = $this->pageInfo['cate_id'];
		}
		
		if($this->pageInfo['status'] = I('status', '')){
			$map['status'] = $this->pageInfo['status'];
		}
		
		//获得列表
		$list = $this->modelObject->getPageList($map, $thisPage);
		
		//分页
		$page = $this->modelObject->buildPage($map, $thisPage);
		$this->assign('page',$page);
		
		session('pageInfo', $this->pageInfo);
		$this->assign('pageInfo', $this->pageInfo);
		$this->assign('list', $list);
		
		$this->assign('categoryTextArray', D('WebsitesCate')->getAllCateTextArray());
		
		$this->display();
    }
	
	public function recycle(){
		$thisPage = $this->pageRecycleInfo['p'] = I('p', 1);
		$map = array();
		$map['is_recycle'] = 1;
		
		if($this->pageRecycleInfo['name'] = I('name', '')){
			$map['name'] = array('LIKE', '%'.$this->pageRecycleInfo['name'].'%');
		}
		
		if($this->pageRecycleInfo['cate_id'] = I('cate_id', '')){
			$map['cate_id'] = $this->pageRecycleInfo['cate_id'];
		}
		
		//获得列表
		$list = $this->modelObject->getPageList($map, $thisPage);
		
		//分页
		$page = $this->modelObject->buildPage($map, $thisPage);
		$this->assign('page',$page);
		
		session('webapplyRecyclePageInfo', $this->pageRecycleInfo);
		$this->assign('pageRecycleInfo', $this->pageRecycleInfo);
		$this->assign('list', $list);
		
		$this->assign('categoryTextArray', D('WebsitesCate')->getAllCateTextArray());
		
		$this->display();
    }
	
	public function del(){
		$id = I('id', 0, 'intval');
		if($id){
			if($this->modelObject->deleteInfo($id)){
				$this->success('删除成功', U('recycle', session('webapplyRecyclePageInfo')));	
			}else{
				$this->error('删除失败!');
			}
		}else{
			$this->error('参数错误!');
		}
	}
	
	public function delAll(){
		$id = I('id', 0, 'intval');
		$idStr = implode(',', $id);
		if(!empty($idStr)){
			if($this->modelObject->deleteInfo($idStr)){
				$this->success('删除成功', U('recycle', session('webapplyRecyclePageInfo')));	
			}else{
				$this->error('删除失败!');
			}
		}else{
			$this->error('参数错误!');
		}
	}
	
	public function nodel(){
		$id = I('id', 0, 'intval');
		if($id){
			if($this->modelObject->where(array('id'=>$id))->setField('is_recycle', 1)){
				$this->success('操作成功', U('index', session('pageInfo')));	
			}else{
				$this->error('操作失败!');
			}
		}else{
			$this->error('参数错误!');
		}
	}
	
	public function nodelAll(){
		$id = I('id', 0, 'intval');
		$idStr = implode(',', $id);
		if(!empty($idStr)){
			if($this->modelObject->where(array('id'=>array('IN',$idStr)))->setField('is_recycle', 1)){
				$this->success('操作成功', U('index', session('pageInfo')));	
			}else{
				$this->error('操作失败!');
			}
		}else{
			$this->error('参数错误!');
		}
	}
	
	/*批量入库*/
	public function setIn(){
		$id = I('id', 0, 'intval');
		$idStr = implode(',', $id);
		if(!empty($idStr)){
			//获取到所有选中的信息
			if($list = $this->modelObject->where(array('id'=>array('IN',$idStr)))->select()){
				foreach($list as $key=>$val){
					$id = $val['id'];
					unset($val['id']);
					$val['status'] = 1;
					if($insertId = $this->websitesObject->add($val)){
						//增加到附表
						$data = array();
						$data['web_id']      = $insertId;
						$data['update_time'] = NOW_TIME;
						$this->webdataObject->add($data);
						
						$this->modelObject->delete($id);
					}
				}	
			}
			
			$this->success('操作成功', U('index', session('pageInfo')));	
		}else{
			$this->error('参数错误!');
		}
	}
	
	public function reset(){
		$id = I('id', 0, 'intval');
		if($id){
			if($this->modelObject->where(array('id'=>$id))->setField('is_recycle', 2)){
				$this->success('操作成功', U('recycle', session('webapplyRecyclePageInfo')));	
			}else{
				$this->error('操作失败!');
			}
		}else{
			$this->error('参数错误!');
		}
	}
	
	public function resetAll(){
		$id = I('id', 0, 'intval');
		$idStr = implode(',', $id);
		if(!empty($idStr)){
			if($this->modelObject->where(array('id'=>array('IN',$idStr)))->setField(array('is_recycle'=>2))){
				$this->success('操作成功', U('recycle', session('webapplyRecyclePageInfo')));	
			}else{
				$this->error('操作失败!');
			}
		}else{
			$this->error('参数错误!');
		}
	}
	
	public function setStatus(){
		$id = I('id', 0, 'intval');
		$statusMessage = I('status_message');
		
		if($id > 0 && !empty($statusMessage)){
			if( $this->modelObject->where(array('id'=>$id))->setField(array('status'=>2, 'status_message'=>$statusMessage))){
				$this->ajaxReturn(1, '拒绝成功', 1);
			}else{
				$this->ajaxReturn(0, '拒绝失败', 0);
			}
		}else{
			$this->ajaxReturn(0, '参数错误', 0);
		}
	}
}//end class